<script src="//maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=<?php echo $this->setting_model->item('googlemap_api')?>"
type="text/javascript"></script>
<script type="text/javascript">
  var map, marker;
  
  function create_marker(point){
    marker = new GMarker(point, {draggable: true});
    GEvent.addListener(marker, "dragstart", function() {
      //map.closeInfoWindow();
    });
    GEvent.addListener(marker, "dragend", function() {
      var point = marker.getPoint();
      map.panTo(point);
      jQuery("#lat").val( point.lat().toFixed(5) );
      jQuery("#lng").val( point.lng().toFixed(5) );
    });
    map.addOverlay(marker);
  }
  function initialize() {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      //map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.addControl(new GScaleControl());
      map.enableScrollWheelZoom();
      var customUI = map.getDefaultUI();
      customUI.maptypes.hybrid = false;
      map.setUI(customUI);
      var center = new GLatLng( <?php echo isset($lat) ? $lat: '37.4419' ?>, 
                                <?php echo isset($lng) ? $lng: '-122.1419' ?>);
      map.setCenter(center, 13);
      create_marker(center);
    }
  }
  var country_id = 0;
  jQuery(document).ready(function(){
    initialize();    
    $('#country_id').bind({
      change: function (){detect_country($(this).val());},
      blur: function (){detect_country($(this).val());}
    });
      
  });
  function detect_country(id){
    if(country_id != id){
      country_id = id;
      if(id){
        $.post('<?php echo site_url('admin/ajax/get_json_country') ?>', {id : id}, function(data){
          map.clearOverlays();
          var center = new GLatLng( <?php echo isset($lat) ? $lat: '37.4419' ?>, 
                                <?php echo isset($lng) ? $lng: '-122.1419' ?>);
          if(data){
            $('#lat').val(data.lat);
            $('#lng').val(data.lng);            
            center = new GLatLng( data.lat,data.lng);
          }
          map.setCenter(center);
          create_marker(center);
        }, 'json');
      }
    }
  }
  
</script>